package net.lingala.zip4j.model;

import net.lingala.zip4j.exception.ZipException;

/**
 * Created by gelon on 2017/6/30.
 */
public class ByteVirtualFile {
    byte[] bs ;
    long index = 0;

    public ByteVirtualFile(byte[] bs) {
        this.bs = bs;
    }

    public int read(byte[] buf)  {
        if(index + buf.length <= bs.length){
            System.arraycopy(bs,(int)index,buf,0,buf.length);
            index = index+ buf.length;
            return buf.length;
        } else {
            int real = (int)(bs.length - index) ;
            System.arraycopy(bs,(int)index,buf,0,real);
            index = index + real;
            return real;
        }
    }

    public int read(byte[] buf,int offset,int len){
        if(index + len <= bs.length){
            System.arraycopy(bs,(int)index,buf,offset,len);
            index = index+ len;
            return len;
        } else {
            int real = (int)(bs.length - index) ;
            System.arraycopy(bs,(int)index,buf,offset,real);
            index = index + real;
            return len;
        }
    }




    public void seek(long index){
        this.index = index;
    }

    public long getFilePointer(){
        return index;
    }

    public int length(){
        return bs.length;
    }


    public void close() {
    }
}
